﻿using System;
using System.Collections.Generic;
using System.Text;

namespace VOAEnglishDownloaderConsole
{

    public class Program
    {
        private SharpDownloadSDK.HttpWebRequestDownloader downloader = new SharpDownloadSDK.HttpWebRequestDownloader();
        private VOAEnglishDownload.VOASpecialEnglish voaUrls = new VOAEnglishDownload.VOASpecialEnglish();

        private String savePath = String.Empty;
        private int year = 0;

        public Program()
        {
            downloader.DownloadFileCompletedEvent += new SharpDownloadSDK.DownloadFileCompleted(DownLoadFileCompletedEventHandler);
            voaUrls.AnalyseFileCompletedEvent1 += new VOAEnglishDownload.VOASpecialEnglish.AnalyseFileCompletedEvent(AnalyseFileCompletedEventHandler);
        }

        public void Download()
        {
            voaUrls.SaveDirectory = savePath;
            voaUrls.VOAYear = year;
            voaUrls.GetUrls();
        }
        public bool GetUserInput()
        {
            try
            {
                Console.WriteLine("请输入保存路径：");
                String savePath = Console.ReadLine();
                if (!System.IO.Directory.Exists(savePath))
                {
                    System.IO.Directory.CreateDirectory(savePath);
                    this.savePath = savePath;
                }
                Console.WriteLine("请输入要下载的年，必须在2001到2007之间：");
                String yearStr = Console.ReadLine();
                int year = int.Parse(yearStr);
                if (year >= 2001 && year <= 2007)
                {
                    this.year = year;
                }
                else
                    return false;
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static void Main()
        {
            Program a = new Program();
            if (!a.GetUserInput())
                return;

            a.Download();
            System.Threading.Thread.CurrentThread.Suspend();
        }

        private void AnalyseFileCompletedEventHandler(String url, String fullPath)
        {
            if (!downloader.UrlAndSavePaths.ContainsKey(url))
            {
                downloader.UrlAndSavePaths.Add(url, fullPath);
                downloader.DownloadFileAsync(url, fullPath);
            }
        }

        private void DownLoadFileCompletedEventHandler(Object sender, SharpDownloadSDK.DownloadFileCompletedEventArgs e)
        {
            if (downloader.CurrentCompletedFileNum == downloader.UrlAndSavePaths.Count)
            {
                Console.BackgroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("下载可能完成！");
            }
            String currentFileNum = "(" + downloader.CurrentCompletedFileNum.ToString() + "/" + downloader.UrlAndSavePaths.Count.ToString() + ")";
            
            String currentFileResult = String.Empty;
            if (e.IsSuccessful)
            {
                currentFileResult = "下载成功：" + e.SavePath; 
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(currentFileNum + currentFileResult);
            }
            else
            {
                currentFileResult = "下载失败：" + e.SavePath;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(currentFileNum + currentFileResult);
            }

            


        }
    }
}
